package com.ruoyi.crm.sale.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.sale.mapper.CrmTeamMapper;
import com.ruoyi.crm.sale.domain.CrmTeam;
import com.ruoyi.crm.sale.service.ICrmTeamService;

/**
 * 团队，针对客户或商机进行服务Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-08
 */
@Service
public class CrmTeamServiceImpl implements ICrmTeamService 
{
    @Autowired
    private CrmTeamMapper crmTeamMapper;

    /**
     * 查询团队，针对客户或商机进行服务
     * 
     * @param id 团队，针对客户或商机进行服务ID
     * @return 团队，针对客户或商机进行服务
     */
    @Override
    public CrmTeam selectCrmTeamById(Long id)
    {
        return crmTeamMapper.selectCrmTeamById(id);
    }

    /**
     * 查询团队，针对客户或商机进行服务列表
     * 
     * @param crmTeam 团队，针对客户或商机进行服务
     * @return 团队，针对客户或商机进行服务
     */
    @Override
    public List<CrmTeam> selectCrmTeamList(CrmTeam crmTeam)
    {
        return crmTeamMapper.selectCrmTeamList(crmTeam);
    }

    /**
     * 新增团队，针对客户或商机进行服务
     * 
     * @param crmTeam 团队，针对客户或商机进行服务
     * @return 结果
     */
    @Override
    public int insertCrmTeam(CrmTeam crmTeam)
    {
        crmTeam.setCreateTime(DateUtils.getNowDate());
        crmTeam.setUpdateTime(DateUtils.getNowDate());
        crmTeam.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        return crmTeamMapper.insertCrmTeam(crmTeam);
    }

    /**
     * 修改团队，针对客户或商机进行服务
     * 
     * @param crmTeam 团队，针对客户或商机进行服务
     * @return 结果
     */
    @Override
    public int updateCrmTeam(CrmTeam crmTeam)
    {
        crmTeam.setUpdateTime(DateUtils.getNowDate());
        crmTeam.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        return crmTeamMapper.updateCrmTeam(crmTeam);
    }

    /**
     * 批量删除团队，针对客户或商机进行服务
     * 
     * @param ids 需要删除的团队，针对客户或商机进行服务ID
     * @return 结果
     */
    @Override
    public int deleteCrmTeamByIds(Long[] ids)
    {
        return crmTeamMapper.deleteCrmTeamByIds(ids);
    }

    /**
     * 删除团队，针对客户或商机进行服务信息
     * 
     * @param id 团队，针对客户或商机进行服务ID
     * @return 结果
     */
    @Override
    public int deleteCrmTeamById(Long id)
    {
        return crmTeamMapper.deleteCrmTeamById(id);
    }

    /**
     * 查询不在当前团队的所有人员
     * 
     * @param crmTeam 团队，针对客户或商机进行服务
     * @return 团队，针对客户或商机进行服务集合
     */
    public List<Map<String, Object>> getNotTeamUser(CrmTeam crmTeam){
    	return crmTeamMapper.getNotTeamUser(crmTeam);
    }

    /**
     * 设置负责人
     * 判断是否有负责人，如果有，且是当前查询用户，不处理；如果没有进行新增；如果有但不是当前用户，删除后新增
     * 
     * @param crmTeam 团队，针对客户或商机进行服务
     * @return 团队，针对客户或商机进行服务集合
     */
    public void setLeader(CrmTeam crmTeam){
    	List<CrmTeam> leaderList = crmTeamMapper.getCrmLeaderTeamList(crmTeam);
    	if(leaderList.size() == 0 || leaderList.size() > 1) {
    		if(leaderList.size() > 1) {
    			for(CrmTeam team : leaderList) {
        			crmTeamMapper.deleteCrmTeamById(team.getId());
        		}
    		}
    		
    		crmTeam.setCreateTime(DateUtils.getNowDate());
            crmTeam.setUpdateTime(DateUtils.getNowDate());
            crmTeam.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
            crmTeam.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    		crmTeamMapper.insertCrmTeam(crmTeam);
    	}else if(leaderList.size() == 1){
    		if(leaderList.get(0).getUserId() != crmTeam.getUserId()) {
    			leaderList.get(0).setUserId(crmTeam.getUserId());
                crmTeam.setUpdateTime(DateUtils.getNowDate());
                crmTeam.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    			crmTeamMapper.updateCrmTeam(leaderList.get(0));
    		}
    	}
    }
}
